*============================================================================================================================*
* Task: calculate the migration rates and assign a migration rate for each infant mortality site based on 2005 Micro-Census 
*============================================================================================================================*

#delimit ; 

set more off ;     pause on ;      

global path xxxx ;  // set path here   

tempfile f1 ; 

*** Unweighted ; 

use $path\pdata1_num_ren.dta, clear ;    
recode sex (8 9=.) ;  	* 1 male, 2 female ;   
gen prov_id=int(region/10000) ;  *province id ; 
gen pref_id=int(region/100) ;    *prefecture id ;   

gen code=region ; 
tab code if code==410881 | code==411800 | code==419999 ;   
recode code (410881 411800 419999=419001) ;     // 河南省省直管济源市 (2017 new administrative code) 
recode code (341421=340124) ;     // 安徽省合肥市庐江县 (2017 new administrative code) 

merge m:1 code using $path\IMR_sites.dta ; 
list census2010_code jcdmc code if _merge==2 ; 

/* 
+----------------------------------------------------+
| census~e                            jcdmc     code |
|----------------------------------------------------|
|   320603               江苏省南通市开发区   320613 |
|   460202               海南省三亚市河东区   460202 |
|   460203               海南省三亚市河西区   460203 |
+----------------------------------------------------+
*/ ;   


keep if _merge==3 ; 

gen all=1 ;  


*** check non-hukou long-term (>6 months) residents ;  

recode time_out (4/9=4) ; 

preserve ; 

forvalues i=2/4 { ; 
	gen noHukou`i'=. ; 
	replace noHukou`i'=1 if time_out==`i'  ; 
} ; 
collapse (sum) all noHukou* [pweight=power_2], by(code) ;  
sum, sep(0) ; 

forvalues i=2/4 { ; 
	gen mig_pct`i'=noHukou`i'/all ; 
} ; 

replace mig_pct2=mig_pct2+mig_pct3+mig_pct4  ;   
replace mig_pct3=mig_pct3+mig_pct4  ;   

keep code mig* ; 
label var mig_pct2 "percentage of migrants" ; 
label var mig_pct3 "percentage of migrants > 6 months" ;  
label var mig_pct4 "percentage of migrants > 12 months" ;   

save $path\mig_pattern, replace ;    

restore ; 


keep if sex==2 & (age>=15&age<=50) ;  

forvalues i=2/4 { ; 
	gen noHukou`i'=. ; 
	replace noHukou`i'=1 if time_out==`i'  ; 
} ; 
collapse (sum) all noHukou* [pweight=power_2], by(code) ;  
sum, sep(0) ; 

forvalues i=2/4 { ; 
	gen mig_pct`i'=noHukou`i'/all ; 
} ; 

replace mig_pct2=mig_pct2+mig_pct3+mig_pct4  ;   
replace mig_pct3=mig_pct3+mig_pct4  ;   

keep code mig* ; 
label var mig_pct2 "percentage of migrants" ; 
label var mig_pct3 "percentage of migrants > 6 months" ;  
label var mig_pct4 "percentage of migrants > 12 months" ;   


ren mig_pct2 BW_mig_pct2 ;   
ren mig_pct3 BW_mig_pct3 ;   
ren mig_pct4 BW_mig_pct4 ;   

merge 1:1 code using $path\mig_pattern ;   
drop _merge ; 
save $path\mig_pattern, replace ;    






